<?php
class Files extends Admin_Controller
{
	public function __construct()
	{
		parent::__construct();
		$this->load->model('file_model');
		$this->load->model('file_item_model');
	}
	
	public function index()
	{
		$file_id = $this->db->getone('SELECT ID FROM FILES WHERE NAME = ?', array('center'));
		if(empty($file_id))
		{
			$dbs = array(
				'name' => 'center'
			);
			$this->file_model->save($dbs);
		}
		$data = $this->file_model->get_by_name('center', $_GET['search']);
		$this->template->build('index', $data);
	}
	
	public function form($id = NULL)
	{
		$data['rs'] = $this->file_item_model->get_row($id);
		$this->template->build('form', $data);
	}
	
	public function save()
	{
		if($_POST)
		{
			ini_set("max_execution_time","600");
			ini_set("memory_limit","12M");
			$data = array(
				'id' => $_POST['id'],
				'file_id' => $_POST['file_id'],
				'filename' => $_POST['filename']
			);
			if($_FILES['src']['name'])
			{
				$data['src'] = $this->file_item_model->upload($_FILES['src'], 'uploads/doc/');	
			}
			$this->file_item_model->save($data);
			set_notify('success', lang('save_data_complete'));
		}
		redirect('files');
	}
	
	public function delete($id)
	{
		if($id)
		{
			$this->file_item_model->delete_file($id, 'uploads/doc/', 'src');
			$this->file_item_model->delete($id);
			set_notify('success', lang('delete_data_complete'));
		}
		redirect('files');
	}
	
	public function download($id)
	{
		$this->load->helper('download');
		$src = $this->file_item_model->get_one('src', $id);
		ini_set("max_execution_time","600");
		ini_set("memory_limit","12M");
		$data = file_get_contents('uploads/doc/'.$src);
		force_download($src, $data); 
	}
	
	public function list_file($fund_id, $year, $month, $agency_id, $name,$show = FALSE)
	{
		$data['files'] = $this->db->getarray('SELECT FILE_ITEMS.* FROM FILES JOIN FILE_ITEMS ON FILE_ITEMS.FILE_ID = FILES.ID WHERE FUND_ID = ? AND YEAR_RECORD = ? AND MONTH_RECORD = ? AND AGENCY_ID = ? AND NAME = ?', array($fund_id, $year, $month, $agency_id, $name));
		dbConvert($data['files']);
		if($show == FALSE) $this->load->view('list_file', $data);
		else $this->load->view('list_file_info', $data);
	}
	
	public function save_file($fund_id, $year, $month, $agency_id, $name, $field = 'src')
	{
		$file_id = $this->db->getone('SELECT ID FROM FILES WHERE FUND_ID = ? AND YEAR_RECORD = ? AND MONTH_RECORD = ? AND AGENCY_ID = ?', array($fund_id, $year, $month, $agency_id));
		if(empty($file_id))
		{
			$data = array(
				'fund_id' => $fund_id,
				'year_record' => $year,
				'month_record' => $month,
				'agency_id' => $agency_id,
				'name' => $name 
			);
			$file_id = $this->file_model->save($data);
		}
		$data = NULL;
		foreach($_FILES[$field]['name'] as $key => $filename)
		{
			if(!empty($filename))
			{
				$ext = pathinfo($filename, PATHINFO_EXTENSION);
				$new_filename = time().$key.'.'.$ext;
				if(copy($_FILES['src']['tmp_name'][$key], 'uploads/doc/'.$new_filename))
				{
					$data = array(
						'file_id' => $file_id,
						'filename' => $_POST['filename'][$key]
					);
					$data['src'] = $new_filename;
					$this->file_item_model->save($data);
				}
			}
		}
	}
	
	public function information_list_file($id, $show = FALSE)
	{
		$data['files'] = $this->db->getarray("SELECT FILE_ITEMS.* FROM FILES JOIN FILE_ITEMS ON FILE_ITEMS.FILE_ID = FILES.ID WHERE FILES.FUND_ID = ? AND FILES.NAME = 'information'", array($id));
		dbConvert($data['files']);
		if($show == FALSE) $this->load->view('list_file', $data);
		else $this->load->view('list_file_info', $data);
	}
	
	public function information_save_file($id)
	{
		$file_id = $this->db->getone('SELECT ID FROM FILES WHERE FILES.ID = ?', array($id));
		if(empty($file_id))
		{
			$data = array(
				'fund_id' => $id,
				'name' => 'information' 
			);
			$file_id = $this->file_model->save($data);
		}
		$data = NULL;
		foreach($_FILES['src']['name'] as $key => $filename)
		{
			if(!empty($filename))
			{
				$ext = pathinfo($filename, PATHINFO_EXTENSION);
				$new_filename = time().$key.'.'.$ext;
				if(copy($_FILES['src']['tmp_name'][$key], 'uploads/doc/'.$new_filename))
				{
					$data = array(
						'file_id' => $file_id,
						'filename' => $_POST['filename'][$key]
					);
					$data['src'] = $new_filename;
					$this->file_item_model->save($data);
				}
			}
		}
	}
}
